#!/bin/sh
#desc:Select storage drive
#type:local

if test -z "$1"; then
	echo "#WARN This script is experimental"
	echo "#> Select the storage drive"
	echo "list: `basename $0`"
	for i in /mnt/*; do
		B=`basename $i`
		CFG="/mnt/$B/opendomo.cfg"
		if test "$B" != "odconf" && test -w "$CFG"; then
			if test -f $CFG && grep -q "CONFDEVICE" $CFG; then
				echo "	-$B	$B	drive selected"			
			else
				echo "	-$B	$B	drive"
			fi
		fi
	done
else
	DRV="$1"
	echo 'CONFDEVICE="1"' >> /mnt/$DRV/opendomo.cfg
	if test -w /mnt/odconf/opendomo.cfg; then
		# Desmarcar como unidad de configuración la anterior: /mnt/odconf/
		sed 's/CONFDEVICE="1"/CONFDEVICE="0"/g' -i /mnt/odconf/opendomo.cfg
	else
		# El archivo es de solo lectura, probablemente el CD.
		# Tendremos que indicar un repositorio válido
		echo "http://opendomo.org/files/pkg" > /etc/opendomo/repositories.cfg
	fi
	sync
	if test -d /mnt/odconf; then
		cp /mnt/odconf/sysconf/sysconf.tar /tmp/sysconf.tmp
		rm /mnt/odconf
	fi
	ln -s /mnt/$DRV /mnt/odconf
	if test -f /tmp/sysconf.tmp; then
		mv /tmp/sysconf.tmp /mnt/odconf/sysconf/sysconf.tar
		chown admin /mnt/odconf/sysconf/sysconf.tar 2>/dev/null
	fi
	if ! test -d /mnt/odconf/pkgcache; then
		mkdir -p /mnt/odconf/pkgcache/ 2>/dev/null
		chown admin /mnt/odconf/pkgcache 2>/dev/null
	fi
	for i in /var/pkg/installed/*; do
		echo "00000" > $i
	done
	/usr/local/opendomo/updateInstalledPackages.sh >/dev/null
	echo "# Drive selected"
	echo "list:`basename $0`"
	echo "	$1	$1	drive"
	echo "actions:"
	echo "	saveSystemConfiguration.sh	Save system configuration"
	echo
fi
